//
//  KandyAdpter.m
//  AwesomeProject
//
//  Created by aiquantong on 5/10/16.
//  Copyright © 2016 Facebook. All rights reserved.
//

#import "KandyAdpter.h"

#import "CustomSDKLogger.h"
#import "CustomerSDKLoggerFormatter.h"

#import "AccessManager.h"
#import "CallManager.h"
#import "ConferenceManager.h"


static KandyAdpter *shareInstance = nil;


@interface KandyAdpter()

@end

@implementation KandyAdpter

@synthesize apiKey,apiSecert;


+(void) saveKey:(NSString *)key Value:(NSString*)val
{
  NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];
  [settings removeObjectForKey:key];
  [settings setObject:val forKey:key];
  [settings synchronize];
}

+(NSString *) getValFormKey:(NSString *)key
{
  NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];
  return [settings objectForKey:key];
}

+(NSString *)getDateFromate:(NSDate *)date dateFromat:(NSString *)dateFromat
{
  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  [dateFormatter setDateFormat:dateFromat];
  NSString *currentDateStr = [dateFormatter stringFromDate:date];
  return currentDateStr;
}


+(NSString *)getTimeFromate:( NSTimeInterval)interval
{
  return [NSString stringWithFormat:@"%d:%d%d", (int)(interval/60)/60, (int)(interval/60)%60, (int)interval%60];
}



+(void)setEnableLog:(BOOL)isConsoleLog fileLog:(BOOL)isFileLog;
{
    is_kandy_console_debug = isConsoleLog;
    is_kandy_file_debug = isFileLog;
    
    CustomSDKLogger *customSDKLogger = [[CustomSDKLogger alloc] initWithFormatter:[[CustomerSDKLoggerFormatter alloc] init]];
    [Kandy sharedInstance].loggingInterface = customSDKLogger;
    customSDKLogger.isFileConsole = NO;
    
    NSLog(@"kandysdk version === %@ is_kandy_console_debug == %d  is_kandy_file_debug == %d",[Kandy sharedInstance].version,is_kandy_console_debug,is_kandy_file_debug);
    if (isFileLog) {
        customSDKLogger.isFileConsole = YES;
        [Kandy sharedInstance].globalSettings.isPrintRTCCallLogs = YES;
        
        KDALog(@"kandysdk version === %@", [Kandy sharedInstance].version);
        [[Kandy sharedInstance].loggingInterface logWithLevel:EKandyLogLevel_info
                                                 andLogString:[[NSString alloc] initWithFormat:@"kandy version === %@", [Kandy sharedInstance].version]];
        
    }
}


+(KandyAdpter *)shareInstance
{
  static dispatch_once_t one;
  dispatch_once(&one, ^{
    if (shareInstance == nil) {
      shareInstance = [[KandyAdpter alloc] init];
    }
  });
  return shareInstance;
}


-(void)initKandySDKWithKey:(NSString *)key secret:(NSString *)secret;
{
    //register it in kandycn  first！
    //xbsx.txtechnology.com.cn
    [Kandy initializeSDKWithDomainKey:key domainSecret:secret];
    
    apiKey = key;
    apiSecert = secret;
    
    //kandycn
    [Kandy sharedInstance].globalSettings.kandyServiceHost = Kandy_Host_Url;
    [Kandy sharedInstance].globalSettings.kandyServiceTimeout = 30;
    [Kandy sharedInstance].globalSettings.isAutoRenewSession = YES;
    [Kandy sharedInstance].globalSettings.shouldWakeupDestinationBeforeCall = YES;
    
    [KandyAdpter setEnableLog:is_kandy_console_debug fileLog:is_kandy_file_debug];
    
    [[AccessManager shareInstance] registerKandyNotification];
    [[CallManager shareInstance] registerKandyNotification];
    [[ConferenceManager shareInstance] registerKandyNotification];
}


-(void)reinitKandySDK;
{
    if (apiKey && [apiKey length] > 0 &&
        apiSecert && [apiSecert length] > 0) {
        [Kandy initializeSDKWithDomainKey:apiKey domainSecret:apiSecert];

        //kandycn
        [Kandy sharedInstance].globalSettings.kandyServiceHost = Kandy_Host_Url;
        [Kandy sharedInstance].globalSettings.kandyServiceTimeout = 30;
        [Kandy sharedInstance].globalSettings.isAutoRenewSession = YES;
        [Kandy sharedInstance].globalSettings.shouldWakeupDestinationBeforeCall = YES;
    
        [KandyAdpter setEnableLog:is_kandy_console_debug fileLog:is_kandy_file_debug];
        
        [[AccessManager shareInstance] registerKandyNotification];
        [[CallManager shareInstance] registerKandyNotification];
        [[ConferenceManager shareInstance] registerKandyNotification];
    }
}

-(void)dealloc
{

}


@end









